import 'package:flutter/material.dart';
import 'package:xws/models/user/user_update_obj.dart';
import 'package:xws/state/user_state.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_btn.dart';
import 'package:xws/widgets/app_text_field.dart';
import 'package:xws/widgets/safe_scaffold.dart';

/// 编辑简介
Future<void> editDescription(BuildContext context) async {
  final String? description = await push<String>(
    context,
    const _EditDescription(),
    routeType: RouteType.fade,
  );

  if (description == null) return;

  loading(context);

  final bool r = await updateInfo(UserUpdateObj(description: description));

  close(context);

  if (r) await us.updateBasicInfo();
}

/// 编辑简介
class _EditDescription extends StatelessWidget {
  const _EditDescription({Key? key}) : super(key: key);

  /// 完成
  void _complate(BuildContext context, String des) {
    if (des.trim().isEmpty) {
      ct('简介不能为空');
      return;
    }

    if (des == us.info.description) {
      ct('简介未更改');
      return;
    }

    pop(context, value: des);
  }

  @override
  Widget build(BuildContext context) {
    String des = us.info.description;

    return SafeScaffold(
      titleText: '编辑简介',
      backgroundColor: white(context),
      actions: <Widget>[
        Padding(
          padding: const EdgeInsets.only(right: 10),
          child: AppBtn(text: '完成', onTap: () => _complate(context, des)),
        ),
      ],
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: AppTextField(
          maxLength: 200,
          maxLines: 10,
          hintText: '填写简介',
          initialValue: des,
          autofocus: true,
          onChanged: (String v) => des = v,
          textInputAction: TextInputAction.newline,
          border: OutlineInputBorder(
            borderSide: BorderSide.none,
            borderRadius: BorderRadius.circular(6),
          ),
          fillColor: canvas(context),
        ),
      ),
    );
  }
}
